package com.newly.common.core.service.operLogger.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.newly.common.base.entity.sys.operLogger.dto.OperLoggerDto;
import com.newly.common.base.entity.sys.operLogger.po.OperLoggerPo;
import com.newly.common.mapper.sys.OperLoggerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: newly-parent
 * @remark:
 * @author: guan
 * @create: 2021-12-04 18:08
 */
public abstract class AbstractOperLoggerService {

    @Autowired
    protected OperLoggerMapper operLoggerMapper;

    protected boolean insert(OperLoggerDto operLoggerDto) {
        OperLoggerPo loggerPo = BeanUtil.copyProperties(operLoggerDto, OperLoggerPo.class);
        loggerPo.setCreateTime(DateUtil.date());
        return operLoggerMapper.insert(loggerPo) > 0;
    }

    protected boolean update(OperLoggerDto operLoggerDto) {
        OperLoggerPo loggerPo = BeanUtil.copyProperties(operLoggerDto, OperLoggerPo.class);
        return operLoggerMapper.updateById(loggerPo) > 0;
    }

    protected List<OperLoggerPo> selectByIds(List<String> logIds) {
        return operLoggerMapper.selectBatchIds(logIds);
    }

    protected List<String> parseTenantIds(List<OperLoggerPo> operLoggerPos) {
        if (ObjectUtil.isEmpty(operLoggerPos)) {
            return null;
        }
        return operLoggerPos.stream().map(operLoggerPo -> operLoggerPo.getUserId().toString())
                .collect(Collectors.toList());
    }

    /**
     * 判断是否需要过滤的对象。
     *
     * @param o 对象信息。
     * @return 如果是需要过滤的对象，则返回true；否则返回false。
     */
    @SuppressWarnings("rawtypes")
    protected boolean isFilterObject(final Object o) {
        Class<?> clazz = o.getClass();
        if (clazz.isArray()) {
            return clazz.getComponentType().isAssignableFrom(MultipartFile.class);
        } else if (Collection.class.isAssignableFrom(clazz)) {
            Collection collection = (Collection) o;
            for (Object value : collection) {
                return value instanceof MultipartFile;
            }
        } else if (Map.class.isAssignableFrom(clazz)) {
            Map map = (Map) o;
            for (Object value : map.entrySet()) {
                Map.Entry entry = (Map.Entry) value;
                return entry.getValue() instanceof MultipartFile;
            }
        }
        return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
                || o instanceof BindingResult;
    }

    /**
     * 参数拼装
     */
    protected String argsArrayToString(Object[] paramsArray) {
        String params = "";
        if (paramsArray != null && paramsArray.length > 0) {
            for (Object o : paramsArray) {
                if (ObjectUtil.isNotEmpty(o) && !isFilterObject(o)) {
                    try {
                        Object jsonObj = JSON.toJSON(o);
                        params += jsonObj.toString() + " ";
                    } catch (Exception e) {
                    }
                }
            }
        }
        return params.trim();
    }
}
